package com.example.poemtest.index.util;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.poemtest.Activity.MyApplication;
import com.example.poemtest.index.entity.PoetryBeanTest;
import com.example.poemtest.index.entity.PoetryTakeBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: JHT
 * @date :   2024-4-11 22:13
 */
public class PoetryManage {
    private static final PoetryManage ourInstance = new PoetryManage();
    SQLiteDatabase db;
    public static PoetryManage getInstance() {
        return ourInstance;
    }
    List<PoetryBeanTest> allList = new ArrayList<>();

    private PoetryManage() {
        AssetsDatabaseManager.initManager(MyApplication.getContext());
        AssetsDatabaseManager mg = AssetsDatabaseManager.getManager();
        db = mg.getDatabase("poetry.db");
    }

    @SuppressLint("Range")
    public  List<PoetryBeanTest> queryAllPoetryBeanData() {
        Cursor cursor = db.query("PoetryTable", null, null, null, null, null, null, null);
        List<PoetryBeanTest> list = new ArrayList<>();
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            for (int i = 0; i < cursor.getCount(); i++) {
                int index = cursor.getInt(cursor.getColumnIndex("index"));
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String title = cursor.getString(cursor.getColumnIndex("title"));
                String content = cursor.getString(cursor.getColumnIndex("content"));
                PoetryBeanTest model = new PoetryBeanTest();
                model.setIndex(index);
                model.setName(name);
                model.setTitle(title);
                model.setContent(content);
                list.add(model);
                cursor.moveToNext();
            }
        }
        cursor.close();
        return list;
    }

    public List<PoetryBeanTest> getRandomlyItems(){
        List<PoetryBeanTest> list =  queryAllPoetryBeanData();
        List<PoetryBeanTest> newList = new ArrayList<>();
        for (int i =0; i< 10;i++){
            int randomNumber = (int) (Math.random() * 319);; // 生成1到319的随机数
            newList.add(list.get(randomNumber));
        }
        return newList;
    }

    public List<PoetryTakeBean> getRandomlyTakeBean(){
        List<PoetryBeanTest> list =  getRandomlyItems();
        List<PoetryTakeBean> newList = new ArrayList<>();
        for (PoetryBeanTest bean : list){
            PoetryTakeBean bean1 = new PoetryTakeBean();
            bean1.setTitle("《" +bean.getTitle()+"》" + " --" + bean.getName());
            String[] strings = bean.getContent().split("。");
            if (strings.length >= 2){
                String[] strings1 = strings[0].split("，");
                if (strings1.length >= 2) {
                    bean1.setContent1(strings1[0]);
                    bean1.setContent2(strings1[1]);
                } else {
                    continue;
                }
                String[] strings2 = strings[1].split("，");
                if (strings2.length >= 2) {
                    bean1.setContent3(strings2[0]);
                    bean1.setContent4(strings2[1]);
                } else {
                    continue;
                }
                newList.add(bean1);
            }

        }
        return newList;
    }

    @SuppressLint("Range")
    public  List<PoetryBeanTest> queryByContent(String text) {
        String  sql = "SELECT * FROM PoetryTable WHERE content LIKE '%" + text + "%'" + " order by id desc"; // 构建SQL语句
        Cursor  cursor = db.rawQuery(sql,null);
        List<PoetryBeanTest> list = new ArrayList<>();
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            for (int i = 0; i < cursor.getCount(); i++) {
                int index = cursor.getInt(cursor.getColumnIndex("index"));
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String title = cursor.getString(cursor.getColumnIndex("title"));
                String content = cursor.getString(cursor.getColumnIndex("content"));
                PoetryBeanTest model = new PoetryBeanTest();
                model.setIndex(index);
                model.setName(name);
                model.setTitle(title);
                model.setContent(content);
                list.add(model);
                cursor.moveToNext();
            }
        }
        cursor.close();
        return list;
    }


}
